/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ResourceBundle;

/**
 *
 * @author scriptoff
 */
public class Controleur implements ActionListener,WindowListener{
    private View v;
    private InterfaceModele m;
    private  ResourceBundle rB = null;

    public Controleur(String lang) {
         rB = java.util.ResourceBundle.getBundle("client/lang"+lang);
    }

    
    
    @Override
    public void actionPerformed(ActionEvent ae) {
        if(ae.getActionCommand().equals(View.DMD_ANNUIT)){
            try {
                double montant = Double.parseDouble(v.getMontantPret());
                double taux = Double.parseDouble(v.getTaux());
                int nbannee = Integer.parseInt(v.getNbAnnee());
                double result = m.demandeAnnuit(montant, taux, nbannee);
                v.drawResultDialog(result);
                m.finaliser();
            } catch (NumberFormatException ex) {
                v.drawErrorDialog(rB.getString("LES ENTRÉES NE SONT PAS CORRECTE"));
            } catch (ErreurService ex) {
                v.drawErrorDialog(rB.getString("ERREUR DU SERVICE"));
            }
        } else if(ae.getActionCommand().equals(View.DMD_NB_ANNEE)){
            try {
                double montant = Double.parseDouble(v.getMontantPret());
                double taux = Double.parseDouble(v.getTaux());
                int annuite = Integer.parseInt(v.getAnnuit());
                double result =  m.demandeNbAnnee(montant, taux, annuite); 
                v.drawResultDialog(result);
                m.finaliser();
            } catch (NumberFormatException ex) {
                v.drawErrorDialog(rB.getString("LES ENTRÉES NE SONT PAS CORRECTE"));
            } catch (ErreurService ex) {
                v.drawErrorDialog(rB.getString("ERREUR DU SERVICE"));
            }
        }
    }
    @Override
    public void windowOpened(WindowEvent we) {
    }

    @Override
    public void windowClosing(WindowEvent we) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent we) {
    }

    @Override
    public void windowIconified(WindowEvent we) {
    }

    @Override
    public void windowDeiconified(WindowEvent we) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void windowActivated(WindowEvent we) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void windowDeactivated(WindowEvent we) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setView(View v) {
        this.v = v;
    }

    public void setInterfaceModele(InterfaceModele m) {
        this.m = m;
    }

    public ResourceBundle getResourceBundle() {
        return rB;
    }
      
}
